const apiUrl = "http://localhost:3001/api";

// headers: new Headers({
//     'Content-Type': 'application/json'
// }),
const headers = {
    Accept: "application/json",
    "Content-Type": "application/json; charset=utf-8",
};


function fetchUsers() {
    return fetch(apiUrl + "/users", {}).then(res => res.json());
}

function createUser(user: any) {
    return fetch(apiUrl + "/users", {
        method: "POST",
        body: JSON.stringify(user),
        headers: {...headers},
    }).then(res => res.json());
}

function updateUser(id: number, user: any) {
    return fetch(apiUrl + "/users/" + id, {
        method: "PUT",
        body: JSON.stringify(user),
        headers: {...headers}
    }).then(res => res.json());
}

function deleteUser(id: number) {
    return fetch(apiUrl + "/users/" + id, {
        method: "DELETE",
    }).then(res => res.json());
}


function fetchTasks() {
    return fetch(apiUrl + "/tasks", {}).then(res => res.json());
}

function createTask(task: any) {
    return fetch(apiUrl + "/tasks", {
        method: "POST",
        body: JSON.stringify(task),
        headers: {...headers},
    }).then(res => res.json());
}

function updateTask(id: number, task: any) {
    return fetch(apiUrl + "/tasks/" + id, {
        method: "PUT",
        body: JSON.stringify(task),
        headers: {...headers}
    }).then(res => res.json());
}

function deleteTask(id: number) {
    return fetch(apiUrl + "/tasks/" + id, {
        method: "DELETE",
    }).then(res => res.json());
}

/////////sync/////
function fetchDevices() {
    return fetch(apiUrl + "/sync", {}).then(res => res.json());
}

function fetchSync() {
    return fetch(apiUrl + "/sync", {}).then(res => res.json());
}

function openSync(task: any) {
    return fetch(apiUrl + "/sync/open", {
        method: "POST",
        body: JSON.stringify({}),
        headers: {...headers},
    }).then(res => res.json());
}

function createSync(task: any) {
    return fetch(apiUrl + "/sync", {
        method: "POST",
        body: JSON.stringify(task),
        headers: {...headers},
    }).then(res => res.json());
}

function updateSync(id: number, task: any) {
    return fetch(apiUrl + "/sync/" + id, {
        method: "PUT",
        body: JSON.stringify(task),
        headers: {...headers}
    }).then(res => res.json());
}

function deleteSync(id: number) {
    return fetch(apiUrl + "/sync/" + id, {
        method: "DELETE",
    }).then(res => res.json());
}

export {
    fetchSync,
    fetchDevices,
    createSync,
    openSync,
    updateSync,
    deleteSync,
    fetchUsers,
    createUser,
    updateUser,
    deleteUser,
    fetchTasks,
    createTask,
    updateTask,
    deleteTask
};
